import { defineStore } from 'pinia';
import { getLoginInfo } from '@/apis/user';
import type { LoginParams } from '@/apis/model/userModel';

export const useUserStore = defineStore('user', {
  persist: {
    key: `m:${location.host}:USER_MODEL`,
    storage: window.sessionStorage, // default
  },
  state: () => {
    return {
      userInfo: {},
      token: '',
      permissionList: [] as string[],
    };
  },
  actions: {
    handleLogin(loginParams: LoginParams) {
      return new Promise<void>((resolve, reject) => {
        getLoginInfo(loginParams).then((res) => {
          const { code, message } = res.data;
          if (code === 200) {
            resolve();
          } else {
            reject(message);
          }
        });
      });
    },
  },
});
